
package com.bds.portal.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.springframework.stereotype.Component;

import com.bds.model.Project;
import com.bds.portal.common.result.Page;
import com.bds.portal.util.Const;

@Component
public class ProjectService {

	private static Logger logger = Logger.getLogger(ProjectService.class);

	@Resource
	private NutDao portalDao;

	public Page getProjectList(Long user_id, Integer pageNum, Integer pageSize) {
		Pager pager = portalDao.createPager(pageNum, pageSize);
		Criteria cri = Cnd.cri();
		cri.where().andEquals("user_id", user_id);
		List<Project> list = portalDao.query(Project.class, cri, pager);

		int count = portalDao.count(Project.class, cri);
		Page p = new Page(count, pager.getPageNumber(), pager.getPageSize());
		p.setCode(Const.SUC);
		p.setData(list);
		return p;

	}

	public List<Project> getProjectList(Long user_id) {
		List<Project> list = portalDao.query(Project.class, Cnd.where("user_id", "=", user_id));
		return list;
	}

	public Project saveProject(Long user_id, String projectName, String projectDesc) {

		try {

			Project p = new Project();
			p.setCreate_time(new Date());
			p.setProject_desc(projectDesc);
			p.setProject_name(projectName);
			p.setUser_id(user_id);

			portalDao.fastInsert(p);
			return p;
		} catch (Exception e) {
			logger.error("", e);
			return null;
		}

	}

	public Project getProject(Long id) {
		Project p = portalDao.fetch(Project.class, Cnd.where("id", "=", id));
		return p;
	}

	public Project getProject() {
		List<Project> p = portalDao.query(Project.class, Cnd.orderBy().asc("id"));
		if (p != null && p.size() > 0)
			return p.get(0);
		else
			return null;
	}

	public Project getProjectByName(Long user_id, String project_name) {
		Project p = portalDao.fetch(Project.class,
				Cnd.where("user_id", "=", user_id).and("project_name", "=", project_name));
		return p;
	}

}
